$(document).ready(function(){
	pageInit();
	//导航转换
	$("#createFile").click(function(){
		H.menu('#crtDan','m_virdb_bar');
		$(".formFile").hide();
		$(".danActive").hide();
	});
	$("#fileList").click(function(){
		H.menu('#listsDan','m_virdb_bar');
		$(".danActive").hide();
		updateList();
	});
	$("nav a").click(function(){
		$(".active").removeClass("active");
		$(this).addClass("active");
	});
	//选择文件类型
	$(".modelSuff").click(function(){
		var id = $(this).attr('id');
		$("#suffix").attr("readonly", "readonly");
		var fstruct ={'json':'JSON','xml':'XML','xls':'Excel','csv':'scv','accdb':'accdb'};//固定格式
		var selStr = {'json':'JSON','ser':'PHP序列化','text':'文本格式'};//可选格式
		if(H.empty(id)) $("#suffix").removeAttr("readonly", '');
		if(!H.empty(fstruct[id])) $("#struct").html('<option value="'+id+'">'+fstruct[id]+'</option>');
		else{
			var option = '';
			for(var k in selStr){
				option += '<option value="'+k+'">'+selStr[k]+'</option>';
			}
			$("#struct").html(option);
		}
		$(".formFile").show();
		$("#suffix").val(id);
	});
	//文件存在性检测
	$("#filename").blur(function(){
		var fname = $("#filename").val();
		if(H.empty(fname)) return false;
		var ftype = $("#suffix").val();
		fname += H.empty(ftype)? '' : '.'+ftype;
		$.post(H.APP+'admin/admin.php',{fname:fname,item:"fnameCheck",model:"virtualdb",uid:Math.random()},
			function(data){
				if(1 == data){alert(fname+'文件已经存在，请更换文件名');$("#filename").val('');}
		});
	});
	//文件保存
	$("#saveFile").click(function(){
			if(H.sCheck('#filename',"#formFileTip",'请写“<span style="color:red;">文件名</span>”,<i>大哥</i> !')) return;
			//if(H.sCheck('#suffix',"#formFileTip",'请写“<span style="color:red;">文件后缀</span>”,<i>大哥</i> !')) return;//可生成物后缀名文件
			var fname = $("#filename").val();
			var ftype = $("#suffix").val();
			var fdesc = $("#fdesc").val();
			var fstruct = $('#struct option:selected').val();
			var fstr_desc = $('#struct option:selected').text();
			$.post(H.APP+'admin/admin.php',{fname:fname,ftype:ftype,fdesc:fdesc,fstruct:fstruct,fstr_desc:fstr_desc,item:"saveFile",model:"virtualdb",uid:Math.random()},
				function(data){
					alert(data);
					 $("#filename").val('');
					H.session('m_virdb_filelist','');
			});
	});
	window.onbeforeunload = function(e){
		//清除session的值
		return "刷新或关闭将删除历史信息？";
	};
});
//文件列表事件
$(document).on("click",".files",function(){
	var dir = $(this).attr('_dir');
	var name = $(this).text();
	var c = parseInt($("#morenav").attr("count"))+1;
	$("#morenav").attr("count",c);
	$(".active").removeClass("active");
	$("#morenav").append(' <span id="nav_'+c+'"><a href="javascript:void(0);" _dir="'+dir+'" class="navfname active" id="'+c+'">'+name+'</a><a href="javascript:void(0);" class="colsebtn" _close="#nav_'+c+'" id="colseIcno'+c+'" _no="'+c+'"><sup>×</sup></a></span>');
	var active = "pDance_"+c;
	if($('#'+active).length == 0) $("#pDance").append('<div id="'+active+'" class="danActive"></div>');
	danceActive('#'+active);
	designHtml(c,name);
	//alert(dir);
});
//文件导航事件
$(document).on("click",".navfname",function(){
	$(".active").removeClass("active");
	$(this).addClass("active");
	//var fdir = $(this).attr('_dir');
	var fname = $(this).text();
	var no = $(this).attr("id");
	designHtml(no,fname);
	danceActive('#pDance_'+no);
});
//文件导航鼠标覆盖元素事件
$(document).on("mouseover",".navfname",function(){
	$(".colsebtn").hide();
	var no = $(this).attr("id");
	$("#colseIcno"+no).show();
});
//离开删除按钮则隐藏
$(document).on("mouseout",".colsebtn",function(){$(this).hide();});
 
//关闭指定的导航鼠标点击事件
$(document).on("click",".colsebtn",function(){
	var closeNav = $(this).attr("_close");
	var no = $(this).attr("_no");
	$(closeNav).remove();//删除导航
	$("#pDance_"+no).remove();//删除图层
	$("#listsDan").show();
	$("#fileList").addClass("active");
	H.session('m_virdb_pDance'+no,'');

});
//删除文件
$(document).on("click",".delFiles",function(){
	var r = confirm('您确定要删除文件？');
	if(true == r){
		var dir = $(this).attr("_dir");
		$.post(H.APP+'admin/admin.php',{dir:dir,item:"delFile",model:"virtualdb",uid:Math.random()},
			function(data){
				H.session('m_virdb_filelist','');
				updateList();
		});
	}
});
//数据写入保存
$(document).on("click",".writeSave",function(){
	var write = value('.writCipt');
	if(H.empty(write)){ 
		alert('请填写文本内容！');
		var no = $(".active").attr('id');
		$("#pDance_"+no).find('.writCipt').focus();
		return;
	} 
	value(".pritCipt",write);
	var json = value();
	json['text'] = write;json["model"] = 'virtualdb';json['item'] = 'writeSave';json['uid'] = Math.random();
	$.post(H.APP+'admin/admin.php',json,
		function(data){
			value(".writCipt",'');
			value(".pritCipt",data);
	});
});
//文件写入框同步显示
$(document).on("blur",".writCipt",function(){
	var val = value('.writCipt');
	var print = value('.pritCipt');
	if(H.empty(val)) return;
	print += val;
	value(".pritCipt",print);
});
//快捷键调试事件
$(document).keydown(function(e){
	if(e.altKey && e.keyCode == '65'){//alt+A
		for(var k in sessionStorage){
			if(k.indexOf('m_virdb_pDance')>-1){sessionStorage.removeItem(k);}
		}
		sessionStorage.removeItem('m_virdb_filelist');//文件列表
		alert("已清除图层数据仓！");
	}
	else if(e.shiftKey && e.keyCode == '65'){
		console.log(sessionStorage);
	}
	/*
	ctrlKey,altKey,shiftKey
	*/
});
function pageInit()
{
	H.menu('','m_virdb_bar','#listsDan,#crtDan');
	var bar = H.session('m_virdb_bar');
	updateList();
	//H.session('m_virdb_filelist','');//测试
}
function updateList()
{
	var filelist = H.session('m_virdb_filelist');
	if(H.empty(filelist)){
		$.post(H.APP+'admin/admin.php',{item:"loadFileList",model:"virtualdb",uid:Math.random()},function(data){$("#listsDan").html(data);H.session('m_virdb_filelist',data);});
		filelist = H.session('m_virdb_filelist');
	}
	$("#listsDan").html(filelist);
}
//布景激活
function danceActive(active)
{
	$("#listsDan").hide();
	$("#crtDan").hide();
	$(".danActive").hide();//隐藏所有图层
	if(!H.empty(active)) $(active).show();
}
//布景填充
function designHtml(no,fname)
{
	if(H.empty(no)) return;
	var dsnXhtml = H.session('m_virdb_pDance'+no);
	if(H.empty(dsnXhtml)){
		if(H.empty(fname)) return ;
		$.post(H.APP+'admin/admin.php',{fname:fname,item:"loadModule",model:"virtualdb",uid:Math.random()},
			function(data){
				$('#pDance_'+no).html(data);
				H.session('m_virdb_pDance'+no,data);
		});
	}else{$('#pDance_'+no).html(dsnXhtml);}
}
//获取图层id
function value(sel,value)
{
	var retVal = {};
	var no = $(".active").attr('id');
	if(!H.empty(sel)){
		if(!H.none(value)) $("#pDance_"+no).find(sel).val(value);
		return $("#pDance_"+no).find(sel).val();
	}
	retVal['no'] = no;
	retVal['dir'] = $(".active").attr("_dir");
	retVal['fname'] = $(".active").text();
	return retVal;
}
//图层 jquery风格版html
function html(sel,xhtml)
{
	var no = $(".active").attr('id');
	var layer = "#pDance_"+no;
	if(H.none(xhtml)) return $(layer).find(sel).html();
	$(layer).find(sel).html(xhtml);
}